package com.ruoyi.common.utils.date;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtil {

    public static LocalDateTime str2Date(String dateStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter);
        return dateTime;
    }

    public static LocalDateTime str2Date(String dateStr, DateTimeFormatter formatter) {
        LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter);
        return dateTime;
    }

    public static String date2cnFormat(String dateStr, DateTimeFormatter fromFormatter, DateTimeFormatter toFormatter) {
        if (StringUtils.isBlank(dateStr) || fromFormatter == null || toFormatter == null) {
            return dateStr;
        }

        LocalDateTime dateTime = LocalDateTime.parse(dateStr, fromFormatter);
        String dateTimeStr = dateTime.format(toFormatter);
        //System.out.println(dateTimeStr);  // 输出：yyyy年MM月dd日 HH时mm分ss秒   输出：yyyy年MM月dd日 HH时mm分

        return dateTimeStr;
    }

    /*public static String date2format(String dateStr, String fromFormatterStr, String toFormatterStr) {
        if (StringUtils.isEmpty(fromFormatterStr)) {
            fromFormatterStr = "yyyy-MM-dd HH:mm:ss";
        }

        DateTimeFormatter fromFormatter = DateTimeFormatter.ofPattern(fromFormatterStr);
        DateTimeFormatter toFormatter = DateTimeFormatter.ofPattern(toFormatterStr);

        return date2format(dateStr, fromFormatter, toFormatter);
    }*/


    public static String date2cnFormat(String dateStr) {
        String from = "yyyy-MM-dd HH:mm:ss";
        String to = "yyyy年MM月dd日HH时mm分ss秒";
        if (dateStr.length() < 17) {
            from = "yyyy-MM-dd HH:mm";
            to = "yyyy年MM月dd日HH时mm分";
        } else if (dateStr.length() < 14) {
            from = "yyyy-MM-dd HH";
            to = "yyyy年MM月dd日HH时";
        } else if (dateStr.length() < 11) {
            from = "yyyy-MM-dd";
            to = "yyyy年MM月dd日";
        }
//        System.out.println("toFormatterStr:" + to);
        DateTimeFormatter fromFormatter = DateTimeFormatter.ofPattern(from);
        DateTimeFormatter toFormatter = DateTimeFormatter.ofPattern(to);

        return date2cnFormat(dateStr, fromFormatter, toFormatter);
    }

    public static boolean isDateTime(String str) {
        String pattern = "^\\d{4}[-/]\\d{2}[-/]\\d{2}[\\s\\d{1,2}]{0,}[:\\d{2}]{0,}[:\\d{2}]{0,}$"; // 定义日期时间格式正则表达式
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(str);
        if (m.matches()) {
            //System.out.println(str + " 是合法日期时间格式");
            return true;
        } else {
            //System.out.println(str + " 不是合法日期时间格式");
            return false;
        }
    }
}
